﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CF_DAL_WS.Models;
using CF_DAL_WS.Helpers;

namespace CF_DAL_WS.DTO
{
    public class RestaurantDTO
    {
        public int ID { get; set; }
        public int IDDistrict { get; set; }
        public int IDFoodType { get; set; }
        public Guid IDManager { get; set; }
        public string Name { get; set; }
        public int EstimatedServiceTime { get; set; }
        public string DeliveryDistricts { get; set; }
        public string TimeStart { get; set; }
        public string TimeEnd { get; set; }
        public string Address { get; set; }
        public string Image { get; set; }
        public string ContentType { get; set; }
        public List<MenuDTO> Menus { get; set; }

        public static RestaurantDTO Translate(Restaurant Entity)
        {
            if (Entity != null)
            {
                var dto = new RestaurantDTO
                {
                    ID = Entity.ID,
                    Name = Entity.Name,
                    DeliveryDistricts = Entity.DeliveryDistricts,
                    TimeStart = Entity.TimeStart,
                    TimeEnd = Entity.TimeEnd,
                    Address = Entity.Address,
                    Image = Entity.Image,
                    ContentType = Entity.ContentType,
                    Menus = new List<MenuDTO>()
                };
                if (Entity.IDDistrict != null)
                {
                    dto.IDDistrict = Entity.IDDistrict.Value;
                }
                if (Entity.IDManager != null)
                {
                    dto.IDManager = Entity.IDManager.Value;
                }
                if (Entity.IDFoodType != null)
                {
                    dto.IDFoodType = Entity.IDFoodType.Value;
                }
                if (Entity.EstimatedServiceTime != null)
                {
                    dto.EstimatedServiceTime = Entity.EstimatedServiceTime.Value;
                }
                if (Entity.Menu != null)
                {
                    foreach (var item in Entity.Menu)
                    {
                        GlobalVariables.Logger.Info(item.Name);
                        dto.Menus.Add(MenuDTO.Translate(item));
                    }
                }
                return dto;
            }
            return null;
        }

        public static RestaurantDTO TranslateBase(Restaurant Entity)
        {
            if (Entity != null)
            {
                var dto = new RestaurantDTO
                {
                    ID = Entity.ID,
                    Name = Entity.Name,
                    DeliveryDistricts = Entity.DeliveryDistricts,
                    TimeStart = Entity.TimeStart,
                    TimeEnd = Entity.TimeEnd,
                    ContentType = Entity.ContentType,
                    Address = Entity.Address
                };
                if (Entity.IDManager != null)
                {
                    dto.IDManager = Entity.IDManager.Value;
                }
                if (Entity.IDDistrict != null)
                {
                    dto.IDDistrict = Entity.IDDistrict.Value;
                }
                if (Entity.IDFoodType != null)
                {
                    dto.IDFoodType = Entity.IDFoodType.Value;
                }
                if (Entity.EstimatedServiceTime != null)
                {
                    dto.EstimatedServiceTime = Entity.EstimatedServiceTime.Value;
                }
                return dto;
            }
            return null;
        }

        public static Restaurant Reverse(RestaurantDTO obj)
        {
            var entity = new Restaurant
            {
                Name = obj.Name,
                DeliveryDistricts = obj.DeliveryDistricts,
                TimeStart = obj.TimeStart,
                TimeEnd = obj.TimeEnd,
                Address = obj.Address,
                Image = obj.Image,
                ContentType = obj.ContentType
            };
            if (obj.IDManager != null)
            {
                entity.IDManager = obj.IDManager;
            }
            if (obj.IDDistrict != -1)
            {
                entity.IDDistrict = obj.IDDistrict;
            }
            if (obj.IDFoodType != -1)
            {
                entity.IDFoodType = obj.IDFoodType;
            }
            if (obj.EstimatedServiceTime != -1)
            {
                entity.EstimatedServiceTime = obj.EstimatedServiceTime;
            }
            return entity;
        }

        public void UpdateEntity(ref Restaurant entity)
        {
            entity.ID = ID;
            entity.Name = Name;
            entity.DeliveryDistricts = DeliveryDistricts;
            entity.TimeStart = TimeStart;
            entity.TimeEnd = TimeEnd;
            entity.Address = Address;
            entity.Image = Image;
            entity.ContentType = ContentType;
            if (IDManager != null)
            {
                entity.IDManager = IDManager;
            }
            if (IDDistrict != -1)
            {
                entity.IDDistrict = IDDistrict;
            }
            if (IDFoodType != -1)
            {
                entity.IDFoodType = IDFoodType;
            }
            if (EstimatedServiceTime != -1)
            {
                entity.EstimatedServiceTime = EstimatedServiceTime;
            }
        }
    }
}